package com.example.goodsmanagementcabinet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.goodsmanagementcabinet.R;
import com.example.goodsmanagementcabinet.base.GoodBoxManagerApp_;
import com.example.goodsmanagementcabinet.bean.KeyBean;
import com.example.goodsmanagementcabinet.bean.OverBean;
import com.example.goodsmanagementcabinet.bean.UserBean;
import com.example.goodsmanagementcabinet.db.DatabaseManager;
import com.example.goodsmanagementcabinet.otto.OttoManager;
import com.example.goodsmanagementcabinet.otto.OttoMsg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TakeOverAdapter extends BaseAdapter {
    private Context mContext;
    private List mArrayList = new ArrayList();
    private Map<Integer, Boolean> map = new HashMap<>();

    public TakeOverAdapter(Context context) {
        this.mContext = context;
    }

    public void notifyRefresh(List<OverBean> list) {
        mArrayList.clear();
        mArrayList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return (mArrayList != null && mArrayList.size() == 0) ? 0 : mArrayList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mArrayList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View currentView, ViewGroup arg2) {
        HolderView holderView = null;
        if (currentView == null) {
            holderView = new HolderView();
            currentView = LayoutInflater.from(mContext).inflate(R.layout.item_takeover_userlist, arg2, false);
            holderView.tv_overkeyName = (TextView) currentView.findViewById(R.id.tv_overkeyName);
            holderView.tv_handusername = (TextView) currentView.findViewById(R.id.tv_handusername);
            holderView.tv_handdate = (TextView) currentView.findViewById(R.id.tv_handdate);
            holderView.mRelativeR = (LinearLayout) currentView.findViewById(R.id.mRelativeR);
            holderView.mcheckBox = currentView.findViewById(R.id.mcheckBox);
            holderView.mcheckBox.setButtonDrawable(null);
            currentView.setTag(holderView);
        } else {
            holderView = (HolderView) currentView.getTag();
        }
        final OverBean over = (OverBean) mArrayList.get(position);
        if (over != null) {
            UserBean user = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryUserByusrName(over.getHandoveruserid());
            if (user != null) {
                holderView.tv_handusername.setText(user.getNickname());
            }
            KeyBean key = DatabaseManager.getInstance(GoodBoxManagerApp_.getInstance()).queryKeyByKeyId(over.getOverkeyid());
            if (key !=null) {
                holderView.tv_overkeyName.setText(key.getKeyName());
            }
            holderView.tv_handdate.setText(over.getDate());
        }
        holderView.mcheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    map.put(position, true);
                    OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_CHECK_OVER, over);
                    notifyDataSetChanged();
                } else {
                    map.remove(position);
                    OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_REMOVE_OVER, over);
                    notifyDataSetChanged();
                }
            }
        });
        if (map != null && map.containsKey(position)) {
            holderView.mRelativeR.setBackgroundResource(R.drawable.bg_user_list_ischeck);
            holderView.mcheckBox.setChecked(true);
        } else {
            holderView.mRelativeR.setBackgroundResource(R.drawable.bg_user_list);
            holderView.mcheckBox.setChecked(false);
        }
        return currentView;

    }

    public class HolderView {
        private TextView tv_overkeyName, tv_handusername, tv_handdate;
        private LinearLayout mRelativeR;
        private CheckBox mcheckBox;
    }


}
